package com.sumainfor.zuul.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.sumainfor.common.Constants;
import com.sumainfor.common.entity.SysUserVo;
import com.sumainfor.zuul.entity.SysUserEntity;
import com.sumainfor.zuul.util.HttpContextUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.codec.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * @author PuaChen
 */
@Component
public class AdminZullFilter extends ZuulFilter {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String path = request.getServletPath();
        if (path.startsWith("/sys/")) {
            return true;
        }
        return false;
    }

    /**
     * 这里判断URL权限
     *
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        //判断权限
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();

        RequestContext ctx = RequestContext.getCurrentContext();

        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        //构建Vo 对象
        SysUserVo userVo = new SysUserVo();
        userVo.setAreaId(user.getAreaId());
        userVo.setDeptId(user.getDeptId());
        userVo.setUserId(user.getUserId());
        userVo.setDeptName(user.getDeptName());
        userVo.setLoginName(user.getLoginName());
        userVo.setNo(user.getNo());
        userVo.setName(user.getName());
        userVo.setEmail(user.getEmail());
        userVo.setPhone(user.getPhone());
        userVo.setMobile(user.getMobile());
        userVo.setRoleIdList(user.getRoleIdList());
        userVo.setParentDeptVo(user.getParentDeptVo());
        userVo.setDefaultServiceDeptChild(user.getCenterServiceChild());
        userVo.setBottomDeptList(user.getBottomDeptList());

        try {
            ctx.addZuulRequestHeader(Constants.HEAD_USER_ID, Base64.encodeToString(JSONObject.toJSONString(userVo).getBytes("UTF-8")));
            ctx.addZuulRequestHeader(Constants.HEAD_TYPE, String.valueOf(Constants.HeadType.SYS.getCode()));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

       /* logger.info("转发服务URL ===> {}\n转发附带Header ====> {} \n 参数 ====> {}",
                request.getServletPath(),
                ctx.getZuulRequestHeaders().toString(),
                request.getParameterMap().toString());*/

        logger.debug("转发服务URL ===> {}", request.getServletPath());
        return null;
    }

}
